import router from './router'
import { userStore } from '@/pinia/modules/user'
import { controlStore } from '@/pinia/modules/controller'
import { getUrlParams } from "@/utils/public";
import rongcloud from '@/utils/rongcloud'
    
router.beforeEach( async (to, from) => {
  const useUserStore = userStore();
  const useControlStore = controlStore();
  let {
    user_id = getUrlParams("user_id") || useUserStore.user_id,
    webType = getUrlParams("webType") || useUserStore.webType,
    u_openid = getUrlParams("u_openid") || useUserStore.u_openid,
    room_id = getUrlParams("room_id") || useUserStore.room_id,
  } = to.query
  if(!user_id && to.path!='/404') {
    return {
      path:'/404'
    }
  }
  console.log(user_id);
  console.log(u_openid);

  useUserStore.user_id = user_id;
  useUserStore.u_openid = u_openid;
  useControlStore.user_id = user_id;
  useUserStore.room_id = room_id;
  useUserStore.webType = webType;
  await useUserStore.userSet()
  await useUserStore.getWallet()
  const roomObj = await useControlStore.getGameInfo()
  // console.log('roomObj', roomObj);
  rongcloud(roomObj)
  return true
})

router.afterEach((to) => {
  document.title = to.meta.title as string;
})
